package mmc.ui;

import static mmc.resource.Text12p.glText12p;
import static org.lwjgl.input.Keyboard.KEY_F5;
import static org.lwjgl.opengl.GL11.glColor3f;

import java.util.ArrayList;
import java.util.List;

import org.lwjgl.opengl.GL11;

import mmc.action.Action;
import mmc.action.BartenderKey;
import mmc.action.skill.Skill;

import static org.lwjgl.input.Keyboard.*;

public class SkillWindow extends Window {
	private int item = 0;
	//Map<Integer, Action> map = new HashMap<Integer, Action>();
	public List<Skill> skills = new ArrayList<Skill>();
	@SuppressWarnings("unused")
	private Bartender bar;
	private int columns=5;
	
	public SkillWindow(float pos_x, float pos_y, float size_x, float size_y, Bartender bar) {
		super(pos_x, pos_y, size_x, size_y);
		this.bar = bar;
		this.name = "SkillWindow";
		
		
		init_skills();
		KeyBinding.addWindowShortcut(this.name, KEY_F5, this);
	}

	public void init_skills(){		
		skills.clear();
		skills.addAll(Skill.skills);
	}
	
	@Override
	public void forwardKey(int key, char ch) {
		switch(key){
			case KEY_RIGHT:				
				item ++;
				item %= skills.size(); 
				break;
				
			case KEY_LEFT:
				item --;
				item = (item<0)?skills.size()-1:item;
				break;
				
			case KEY_DOWN:
				item += columns;
				item = Math.min(item, skills.size()-1);
				break;
				
			case KEY_UP:
				item -= columns;
				item = Math.max(item, 0);
				break;
				
			case KEY_RETURN:
				
				break;
				
			default:
				if(KeyBinding.binding.containsKey(key)){
					Action act = KeyBinding.binding.get(key);
					if(act instanceof BartenderKey){
						((BartenderKey)act).action = skills.get(item);
					}
				}							
		}

	}

	@Override
	public void render() {
		if (!open) return;
		
		glColor3f(0.1f, 0.1f, 0.1f); 					//Window
		GL11.glBegin(GL11.GL_QUADS);
		GL11.glVertex2f(x, y);
		GL11.glVertex2f(x, y+height);
		GL11.glVertex2f(x+width, y+height);
		GL11.glVertex2f(x+width, y);
		GL11.glEnd();			
		
		int i = 0, j = 0;
		
		glColor3f(1f, 1f, 1f); 
		for(Skill entry : skills){
			glColor3f(1,  1, 1);
				
			if(entry.texture != null){
				if(item == (i+(j*columns)))
					glColor3f(0,  1, 0);
				
				entry.texture.draw2f(x+(i*0.06f), y+(j*0.06f), 0.05f, 0.05f);												
				i++;
				
				if(i>columns-1){
					i=0;
					j++;
				}
			}
		}
		
		glColor3f(1,1,1);
		glText12p(skills.get(item).name, x+0.01f, y+height/2);	
	
		glText12p("To bind a skill to a bartender button, ", x+0.01f, y+height-0.1f);
		glText12p("choose the skill and then press the ", x+0.01f, y+height-0.07f);
		glText12p("key of the button", x+0.01f, y+height-0.04f);
		
	}
	
	@Override
	public void openWindow()
	{
		init_skills();
		this.open = true;
	}

}
